{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from keras.utils import to_categorical\n",
    "#字符到数字的批量转化\n",
    "def words_2_ints(words):\n",
    "    ints = []\n",
    "    for itmp in words:\n",
    "        ints.append(letter_2_int[itmp])\n",
    "    return ints\n",
    " #数字转为one-hot格式\n",
    "def words_2_one_hot(words, num_classes=num_labels):\n",
    "    return to_categorical(words_2_ints(words), num_classes=num_classes)\n",
    "#获取one-hot格式数据对应数值\n",
    "def get_one_hot_max_idx(one_hot):\n",
    "    idx_ = 0\n",
    "    max_ = 0\n",
    "    for i in range(len(one_hot)):\n",
    "        if max_ < one_hot[i]:\n",
    "            max_ = one_hot[i]\n",
    "            idx_ = i\n",
    "    return idx_\n",
    "#one-hot数据转字符\n",
    "def one_hot_2_words(one_hot):\n",
    "    tmp = []\n",
    "    for itmp in one_hot:\n",
    "        tmp.append(int_2_letter[get_one_hot_max_idx(itmp)])\n",
    "    return \"\".join(tmp)\n",
    " \n",
    "def genarate_data(batch_size=100, genarate_num=-1):\n",
    "    cnt = 0\n",
    "    batch_x = []\n",
    "    batch_y = []\n",
    "    sample_num = 0\n",
    "    while(True):\n",
    "        for i in range(len(data) - sequence_length):\n",
    "            batch_x.append(words_2_one_hot(data[i : i+sequence_length]))\n",
    "            batch_y.append(words_2_one_hot(data[i+sequence_length])[0]) #这里数据加[0]，是为了符合keras的输出数据格式。 因为不加[0]，表示是3维的数据。 你可以自己尝试不加0，看下面的test打印出来是什么\n",
    "            sample_num += 1\n",
    "            #print('sample num is :', sample_num)\n",
    "            if len(batch_x) == batch_size:\n",
    "                yield (np.array(batch_x), np.array(batch_y))\n",
    "                batch_x = []\n",
    "                batch_y = []\n",
    "                if genarate_num != -1:\n",
    "                    cnt += 1 \n",
    "                if cnt == genarate_num:\n",
    "                    return"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
